<template>
  <el-container class="manager-layout">
    <!-- 侧边栏 -->
    <el-aside width="200px">
      <div class="logo">
        <img src="@/assets/logo.jpg" alt="logo">
        <span>秒杀管理系统</span>
      </div>
      <el-menu
        :default-active="$route.path"
        background-color="#304156"
        text-color="#fff"
        active-text-color="#409EFF"
        router
      >
        <el-menu-item index="/manager/dashboard">
          <i class="el-icon-data-analysis"></i>
          <span>数据面板</span>
        </el-menu-item>
        <el-menu-item index="/manager/products">
          <i class="el-icon-goods"></i>
          <span>商品管理</span>
        </el-menu-item>
        <el-menu-item index="/manager/seckill-product">
          <i class="el-icon-goods"></i>
          <span>秒杀商品管理</span>
        </el-menu-item>
        <el-menu-item index="/manager/orders">
          <i class="el-icon-s-order"></i>
          <span>订单管理</span>
        </el-menu-item>
        <el-menu-item index="/manager/users">
          <i class="el-icon-user"></i>
          <span>用户管理</span>
        </el-menu-item>
        <el-menu-item index="/manager/coupons">
          <i class="el-icon-tickets"></i>
          <span>优惠券管理</span>
        </el-menu-item>
        <el-menu-item index="/manager/user-coupons">
          <i class="el-icon-shopping-cart-full"></i>
          <span>用户优惠券</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主要内容区 -->
    <el-container>
      <!-- 头部导航 -->
      <el-header>
        <div class="header-left">
          <i class="el-icon-menu toggle-btn" @click="isCollapse = !isCollapse"></i>
          <breadcrumb />
        </div>
        <div class="header-right">
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="avatar-wrapper">
              <el-avatar :size="30" :src="userInfo.avatar"></el-avatar>
              <span>{{ userInfo.name }}</span>
              <i class="el-icon-arrow-down"></i>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 内容区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { removeToken } from '@/utils/auth'

const router = useRouter()
const isCollapse = ref(false)
const userInfo = ref({
  name: 'Admin',
  avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
})

const handleCommand = async (command) => {
  if (command === 'logout') {
    try {
      await ElMessageBox.confirm('确定要退出登录吗？', '提示')
      removeToken()
      router.push('/login')
    } catch (error) {
      console.log('取消退出')
    }
  }
}
</script>

<style lang="scss" scoped>
.manager-layout {
  height: 100vh;

  .el-aside {
    background-color: #304156;
    color: white;

    .logo {
      height: 60px;
      display: flex;
      align-items: center;
      padding: 0 20px;
      background: #2b2f3a;

      img {
        width: 30px;
        height: 30px;
        margin-right: 10px;
      }

      span {
        font-size: 16px;
        font-weight: 600;
      }
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 1px 4px rgba(0,21,41,.08);

    .header-left {
      display: flex;
      align-items: center;

      .toggle-btn {
        font-size: 20px;
        cursor: pointer;
        margin-right: 20px;
      }
    }

    .header-right {
      .avatar-wrapper {
        display: flex;
        align-items: center;
        cursor: pointer;

        span {
          margin: 0 10px;
        }
      }
    }
  }

  .el-main {
    background-color: #f0f2f5;
    padding: 20px;
  }
}
</style>
